{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, BatchNormalization, Concatenate\n",
    "from keras import backend as K\n",
    "from collections import OrderedDict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=6):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "DATA = OrderedDict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### graph 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "random_seed = 10005\n",
    "data_in_shape = (8, 8, 2)\n",
    "\n",
    "input_layer_0 = Input(shape=data_in_shape)\n",
    "branch_0 = Conv2D(4, (3,3), activation='relu', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True)(input_layer_0)\n",
    "\n",
    "input_layer_1 = Input(shape=data_in_shape)\n",
    "branch_1 = Conv2D(4, (3,3), activation='relu', padding='valid', strides=(1,1), data_format='channels_last', use_bias=True)(input_layer_1)\n",
    "\n",
    "output_layer = Concatenate()([branch_0, branch_1])\n",
    "model = Model(inputs=[input_layer_0, input_layer_1], outputs=output_layer)\n",
    "\n",
    "data_in = []\n",
    "for i in range(2):\n",
    "    np.random.seed(random_seed + i)\n",
    "    data_in.append(np.expand_dims(2 * np.random.random(data_in_shape) - 1, axis=0))\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(random_seed + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(data_in)\n",
    "data_out_shape = result[0].shape\n",
    "data_in_formatted = [format_decimal(data_in[i].ravel().tolist()) for i in range(2)]\n",
    "data_out_formatted = format_decimal(result[0].ravel().tolist())\n",
    "\n",
    "DATA['graph_05'] = {\n",
    "    'inputs': [{'data': data_in_formatted[i], 'shape': data_in_shape} for i in range(2)],\n",
    "    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],\n",
    "    'expected': {'data': data_out_formatted, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### export for Keras.js tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "filename = '../../test/data/graph/05.json'\n",
    "if not os.path.exists(os.path.dirname(filename)):\n",
    "    os.makedirs(os.path.dirname(filename))\n",
    "with open(filename, 'w') as f:\n",
    "    json.dump(DATA, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"graph_05\": {\"inputs\": [{\"data\": [-0.905266, -0.940244, 0.975308, -0.726779, 0.318662, -0.747347, -0.333653, -0.875921, 0.959097, -0.046026, 0.500018, -0.914037, 0.884544, 0.190366, -0.635631, -0.160835, -0.946491, 0.638743, -0.403733, 0.431154, 0.771673, -0.000895, -0.667179, -0.734761, 0.366933, -0.524729, 0.171283, 0.611351, 0.477013, 0.586021, 0.75193, -0.305236, -0.374709, 0.756282, 0.61714, -0.823425, -0.135917, -0.45961, -0.35282, 0.165981, -0.875342, -0.910735, 0.54216, 0.791704, -0.363715, -0.379062, -0.778289, -0.503017, -0.498858, 0.73821, -0.560404, 0.383332, -0.162873, 0.462676, -0.888228, 0.603298, -0.211376, -0.410015, 0.969717, -0.800772, -0.326232, -0.903871, -0.472227, 0.527646, 0.845871, -0.555119, -0.242145, -0.720775, 0.230819, 0.098654, -0.136847, -0.75402, -0.526179, -0.035179, -0.592459, -0.656289, -0.379236, -0.727621, 0.320888, -0.769713, -0.606966, 0.368143, 0.40487, -0.269795, -0.724819, -0.941842, 0.852848, -0.641574, 0.309148, 0.993258, -0.642038, 0.330661, 0.749424, -0.088987, 0.480838, 0.820957, -0.81524, 0.597648, 0.665083, 0.971523, -0.716728, -0.592397, -0.09027, -0.321656, 0.650358, -0.517554, 0.110004, 0.153027, -0.582732, 0.693639, 0.176338, -0.061709, -0.20594, -0.404174, 0.614825, 0.744034, -0.611636, -0.090203, -0.063809, 0.111312, -0.41878, -0.268608, 0.490234, -0.907873, 0.49094, 0.962297, 0.209586, -0.363235], \"shape\": [8, 8, 2]}, {\"data\": [0.87859, 0.511819, -0.499979, 0.899103, -0.273074, -0.448988, 0.778226, -0.725762, 0.533488, -0.254761, 0.563277, 0.149018, -0.800132, -0.323823, -0.687353, -0.853655, 0.344338, -0.804382, -0.339254, -0.814494, -0.931578, -0.554643, 0.526105, 0.197187, -0.423878, -0.86257, -0.805159, 0.875224, -0.45214, -0.480211, -0.152902, 0.569441, -0.211393, -0.317172, -0.578325, 0.08373, -0.290923, -0.917902, -0.79171, -0.507596, -0.688969, 0.13645, -0.227688, -0.984034, -0.12649, 0.788219, 0.613676, 0.486748, 0.810045, 0.79828, 0.258775, -0.365513, 0.780025, -0.883853, 0.036758, -0.106986, 0.678987, -0.117617, 0.719819, -0.904588, -0.003723, 0.744139, 0.344811, -0.832108, -0.69998, 0.185014, 0.256453, -0.047385, -0.870467, -0.492493, 0.739994, 0.674833, 0.475455, -0.095844, -0.920258, 0.895529, 0.933401, 0.086867, -0.084179, 0.058479, -0.698595, 0.109934, -0.497638, -0.759723, 0.184804, -0.097758, -0.482257, 0.052997, -0.120057, -0.599685, -0.324373, -0.776589, 0.197334, -0.039466, 0.911675, -0.34869, 0.506552, 0.716607, 0.930851, -0.101359, 0.28458, 0.811404, 0.177922, 0.483056, 0.348253, -0.838838, 0.761488, -0.285614, 0.177429, -0.170556, 0.726269, -0.285743, 0.532491, -0.26225, 0.773676, 0.539706, 0.911474, 0.962223, 0.343429, 0.446745, 0.643217, -0.443733, -0.897673, -0.237513, 0.051002, 0.698658, -0.294338, -0.358399], \"shape\": [8, 8, 2]}], \"weights\": [{\"data\": [-0.905266, -0.940244, 0.975308, -0.726779, 0.318662, -0.747347, -0.333653, -0.875921, 0.959097, -0.046026, 0.500018, -0.914037, 0.884544, 0.190366, -0.635631, -0.160835, -0.946491, 0.638743, -0.403733, 0.431154, 0.771673, -0.000895, -0.667179, -0.734761, 0.366933, -0.524729, 0.171283, 0.611351, 0.477013, 0.586021, 0.75193, -0.305236, -0.374709, 0.756282, 0.61714, -0.823425, -0.135917, -0.45961, -0.35282, 0.165981, -0.875342, -0.910735, 0.54216, 0.791704, -0.363715, -0.379062, -0.778289, -0.503017, -0.498858, 0.73821, -0.560404, 0.383332, -0.162873, 0.462676, -0.888228, 0.603298, -0.211376, -0.410015, 0.969717, -0.800772, -0.326232, -0.903871, -0.472227, 0.527646, 0.845871, -0.555119, -0.242145, -0.720775, 0.230819, 0.098654, -0.136847, -0.75402], \"shape\": [3, 3, 2, 4]}, {\"data\": [0.87859, 0.511819, -0.499979, 0.899103], \"shape\": [4]}, {\"data\": [-0.719503, 0.396686, 0.710021, -0.058892, -0.78922, -0.74094, -0.421022, -0.808307, 0.800549, -0.334559, -0.186061, 0.852542, -0.759177, -0.565928, -0.439353, -0.566006, 0.739029, 0.513581, -0.267873, 0.743929, -0.767563, -0.052566, -0.04449, 0.68184, 0.572289, 0.515837, -0.674829, -0.345664, 0.702239, -0.638317, -0.083214, -0.666578, 0.712641, -0.391794, 0.056017, -0.858674, -0.008642, -0.159135, -0.00677, 0.331901, -0.257608, -0.824415, 0.571256, 0.741805, -0.75251, 0.468774, -0.784013, 0.681435, -0.23194, 0.101493, -0.128162, -0.603826, -0.49174, -0.065357, -0.32479, 0.631058, 0.083835, -0.769532, -0.682364, 0.882268, -0.833573, -0.460043, 0.514984, 0.945882, -0.716884, -0.795482, 0.426371, -0.988366, 0.643317, 0.599972, -0.313279, -0.44164], \"shape\": [3, 3, 2, 4]}, {\"data\": [0.839584, -0.988988, -0.527734, -0.797693], \"shape\": [4]}], \"expected\": {\"data\": [0.168497, 2.346305, 1.207717, 2.362818, 0.0, 0.070222, 0.126851, 0.0, 0.0, 2.461338, 3.522444, 0.0, 1.109791, 0.293874, 0.0, 0.694899, 0.0, 0.602127, 1.080393, 5.027573, 4.820376, 0.757357, 0.924714, 0.0, 0.840754, 2.573463, 0.0, 0.37957, 1.260478, 1.638501, 0.0, 0.964874, 0.0, 0.0, 1.749803, 0.248222, 1.191638, 0.523066, 0.0, 0.0, 0.204531, 1.745849, 1.276317, 2.429717, 0.0, 0.0, 1.187536, 0.0, 1.683168, 3.2936, 0.0, 1.353053, 0.0, 0.0, 1.321719, 0.0, 1.869343, 0.0, 0.0, 1.239643, 2.580296, 0.451429, 0.0, 0.0, 0.0, 0.503506, 0.0, 3.458984, 1.055342, 0.0, 0.0, 0.0, 2.243518, 0.388835, 0.0, 1.61674, 0.0, 0.0, 1.672239, 0.0, 0.21875, 1.490702, 0.468632, 0.0, 0.734094, 0.0, 0.0, 0.0, 2.308447, 3.05245, 0.116621, 0.0, 0.0, 0.0, 0.029901, 0.316689, 1.570363, 1.094648, 0.0, 0.0, 3.320476, 0.0, 0.0, 0.0, 0.383504, 0.308849, 0.699417, 0.0, 2.189063, 0.0, 0.155778, 0.0, 1.225737, 1.470439, 0.102027, 2.883159, 0.218505, 0.0, 0.57785, 0.90861, 0.0, 1.262406, 0.445399, 2.975429, 3.519903, 0.0, 0.0, 0.0, 2.858283, 4.091212, 0.433764, 1.819933, 0.0, 0.0, 0.0, 0.0, 1.55855, 0.0, 0.406836, 0.201438, 1.342136, 0.0, 0.0, 2.622509, 1.289331, 0.0, 0.0, 2.310345, 2.377774, 0.0, 0.12623, 0.0, 3.804572, 1.310101, 0.0, 0.333002, 2.111372, 0.253786, 0.0, 2.482771, 2.532559, 0.198318, 0.917455, 0.0, 0.926663, 0.0, 1.774883, 0.727605, 0.0, 1.181638, 0.0, 3.146661, 3.865489, 0.427868, 0.0, 0.0, 1.795479, 1.569944, 0.0, 2.244777, 1.144395, 0.0, 0.457962, 1.12195, 0.0, 0.0, 1.76982, 0.0, 1.734202, 0.0, 1.123641, 0.0, 0.0, 0.997504, 1.913048, 1.16456, 0.539119, 0.0, 0.0, 0.0, 0.334853, 1.96839, 0.0, 5.152933, 0.0, 0.313294, 0.0, 0.0, 0.0, 0.0, 0.559432, 0.0, 0.892725, 0.0, 0.0, 1.104554, 0.735698, 0.196587, 0.116801, 1.108088, 0.0, 0.0, 0.180631, 0.0, 0.0, 0.276253, 0.696373, 3.264713, 0.0, 0.0, 0.0, 0.0, 0.0, 1.693181, 0.0, 2.801089, 0.223054, 0.0, 0.0, 0.0, 1.359499, 1.054055, 1.409767, 0.0, 2.004939, 0.0, 0.0, 0.0, 0.0, 2.063701, 0.0, 4.27531, 0.740983, 0.0, 0.0, 1.018859, 0.990622, 1.283051, 0.0, 1.842155, 0.351093, 0.0, 0.353306, 0.0, 1.823133, 0.441465, 0.038059, 0.0, 3.190693, 0.0, 0.0, 0.0, 1.084102, 0.0, 0.0, 0.0, 2.747918, 1.568058, 0.459186, 0.0, 2.068206, 0.0, 0.0, 0.673314, 2.933613, 0.0, 0.0, 2.089611], \"shape\": [6, 6, 8]}}}\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(DATA))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
